home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / directx7 / dstut1.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-10-04  |  13.0 KB  |  351 lines

  1. VERSION 5.00
  2. Begin VB.Form DStutform 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "DS Play Sound"
  5.    ClientHeight    =   3900
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   5505
  9.    Icon            =   "dstut1.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   260
  14.    ScaleMode       =   3  'Pixel
  15.    ScaleWidth      =   367
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin VB.CheckBox chloop 
  18.       Caption         =   "Loop Play"
  19.       Height          =   195
  20.       Left            =   3480
  21.       TabIndex        =   14
  22.       Top             =   1320
  23.       Width           =   1695
  24.    End
  25.    Begin VB.CommandButton cmdStop 
  26.       Cancel          =   -1  'True
  27.       Caption         =   "&Stop"
  28.       Height          =   375
  29.       Left            =   2520
  30.       TabIndex        =   13
  31.       Top             =   1200
  32.       Width           =   750
  33.    End
  34.    Begin VB.CommandButton cmdPause 
  35.       Caption         =   "P&ause"
  36.       Height          =   375
  37.       Left            =   1680
  38.       TabIndex        =   12
  39.       Top             =   1200
  40.       Width           =   750
  41.    End
  42.    Begin VB.CommandButton cmdPlay 
  43.       Caption         =   "&Play"
  44.       Default         =   -1  'True
  45.       Height          =   375
  46.       Left            =   840
  47.       TabIndex        =   11
  48.       Top             =   1200
  49.       Width           =   750
  50.    End
  51.    Begin VB.HScrollBar scrlPan 
  52.       Height          =   255
  53.       LargeChange     =   1000
  54.       Left            =   600
  55.       Max             =   10000
  56.       Min             =   -10000
  57.       SmallChange     =   500
  58.       TabIndex        =   9
  59.       Top             =   840
  60.       Width           =   4215
  61.    End
  62.    Begin VB.HScrollBar scrlVol 
  63.       Height          =   255
  64.       LargeChange     =   20
  65.       Left            =   840
  66.       Max             =   0
  67.       Min             =   -5000
  68.       SmallChange     =   255
  69.       TabIndex        =   6
  70.       Top             =   120
  71.       Width           =   4575
  72.    End
  73.    Begin VB.Label lblmisc 
  74.       AutoSize        =   -1  'True
  75.       BackStyle       =   0  'Transparent
  76.       Caption         =   "Right"
  77.       Height          =   195
  78.       Index           =   3
  79.       Left            =   4920
  80.       TabIndex        =   10
  81.       Top             =   840
  82.       Width           =   375
  83.    End
  84.    Begin VB.Label lblmisc 
  85.       AutoSize        =   -1  'True
  86.       BackStyle       =   0  'Transparent
  87.       Caption         =   "Left"
  88.       Height          =   195
  89.       Index           =   2
  90.       Left            =   120
  91.       TabIndex        =   8
  92.       Top             =   840
  93.       Width           =   270
  94.    End
  95.    Begin VB.Label lblmisc 
  96.       AutoSize        =   -1  'True
  97.       BackStyle       =   0  'Transparent
  98.       Caption         =   "Panning:"
  99.       Height          =   195
  100.       Index           =   1
  101.       Left            =   120
  102.       TabIndex        =   7
  103.       Top             =   480
  104.       Width           =   630
  105.    End
  106.    Begin VB.Label lblmisc 
  107.       AutoSize        =   -1  'True
  108.       BackStyle       =   0  'Transparent
  109.       Caption         =   "Volume:"
  110.       Height          =   195
  111.       Index           =   0
  112.       Left            =   120
  113.       TabIndex        =   5
  114.       Top             =   120
  115.       Width           =   570
  116.    End
  117.    Begin VB.Label Label3 
  118.       AutoSize        =   -1  'True
  119.       BackStyle       =   0  'Transparent
  120.       Caption         =   "An Example of a DX game made in VB."
  121.       Height          =   195
  122.       Index           =   1
  123.       Left            =   1320
  124.       TabIndex        =   4
  125.       Top             =   2400
  126.       Width           =   2760
  127.    End
  128.    Begin VB.Label Label1 
  129.       BackStyle       =   0  'Transparent
  130.       Caption         =   $"dstut1.frx":0442
  131.       Height          =   1215
  132.       Index           =   1
  133.       Left            =   0
  134.       TabIndex        =   3
  135.       Top             =   2640
  136.       Width           =   5415
  137.    End
  138.    Begin VB.Label lblLink1 
  139.       AutoSize        =   -1  'True
  140.       BackStyle       =   0  'Transparent
  141.       Caption         =   "http://www.microsoft.com/directx"
  142.       ForeColor       =   &H00FF0000&
  143.       Height          =   195
  144.       Left            =   120
  145.       MousePointer    =   10  'Up Arrow
  146.       TabIndex        =   2
  147.       Top             =   1680
  148.       Width           =   2385
  149.    End
  150.    Begin VB.Label lbllink2 
  151.       AutoSize        =   -1  'True
  152.       BackStyle       =   0  'Transparent
  153.       Caption         =   "http://www.parkstonemot.freeserve.co.uk/indexfw.htm"
  154.       ForeColor       =   &H00FF0000&
  155.       Height          =   195
  156.       Left            =   120
  157.       MousePointer    =   10  'Up Arrow
  158.       TabIndex        =   1
  159.       Top             =   2160
  160.       Width           =   3900
  161.    End
  162.    Begin VB.Label lbllink3 
  163.       AutoSize        =   -1  'True
  164.       BackStyle       =   0  'Transparent
  165.       Caption         =   "Mailto: Jollyjeffers@GreenOnions.netscapeonline.co.uk"
  166.       ForeColor       =   &H00FF0000&
  167.       Height          =   195
  168.       Left            =   120
  169.       MousePointer    =   10  'Up Arrow
  170.       TabIndex        =   0
  171.       Top             =   1920
  172.       Width           =   3900
  173.    End
  174. Attribute VB_Name = "DStutform"
  175. Attribute VB_GlobalNameSpace = False
  176. Attribute VB_Creatable = False
  177. Attribute VB_PredeclaredId = True
  178. Attribute VB_Exposed = False
  179. Option Explicit
  180. 'DIRECT X 7 is initialised using a reference type library, in VB5, open the PROJECT menu, then select
  181. 'REFERENCES. in the list there will be "DirectX 7 for visual basic type library", with a check next to it.
  182. 'When making your own projects you need to select this library.....
  183. 'Each DX app needs to declare a DX object, similiar to a control, sub objects such as DSound/DDraw are
  184. 'created from this master object:
  185. Dim m_dx As New DirectX7
  186. 'Then there is the sub object, DirectSound:
  187. Dim m_ds As DirectSound
  188. 'Sound is loaded into BUFFERS, these buffers represent different areas of memory on your sound card
  189. 'or system memory. You must have 1 buffer for each .wav file you load. Although you can keep reloading
  190. 'different files into the same buffer, you can only have one file in each buffer at any one time. For
  191. 'this example, we only need one buffer.
  192. Dim m_dsBuffer As DirectSoundBuffer
  193. Dim m_bLoaded As Boolean
  194. 'USED FOR THE LINKS, NOT FOR DX
  195. #If Win32 Then
  196. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
  197. Private Declare Function GetDesktopWindow Lib "user32" () As Long
  198. #Else
  199. Private Declare Function ShellExecute Lib "SHELL" (ByVal hwnd%, ByVal lpszOp$, ByVal lpszFile$, ByVal lpszParams$, ByVal lpszDir$, ByVal fsShowCmd%) As Integer
  200. Private Declare Function GetDesktopWindow Lib "USER" () As Integer
  201. #End If
  202. Private Const SW_SHOWNORMAL = 1
  203. Function StartDoc(DocName As String) As Long
  204.       Dim Scr_hDC As Long
  205.       Scr_hDC = GetDesktopWindow()
  206.       StartDoc = ShellExecute(Scr_hDC, "Open", DocName, "", "C:\", SW_SHOWNORMAL)
  207. End Function
  208. Private Sub Form_Load()
  209.     Me.Show
  210.     On Local Error Resume Next
  211.     'First we have to create a DSound object, this must be done before any features can be used.
  212.     'It must also be done before we set the cooperativelevel or create any buffers.
  213.     Set m_ds = m_dx.DirectSoundCreate("")
  214.     'This checks for any errors, if there are no errors the user has got DX7 and a functional sound card
  215.     If Err.Number <> 0 Then
  216.         MsgBox "Unable to start DirectSound. Check to see that your sound card is properly installed"
  217.         End
  218.     End If
  219.     'THIS MUST BE SET BEFORE WE CREATE ANY BUFFERS
  220.     'associating our DS object with our window is important. This tells windows to stop
  221.     'other sounds from interfering with ours, and ours not to interfere with other apps.
  222.     'The sounds will only be played when the from has got focus....
  223.     'DSSCL_PRIORITY=no cooperation, exclusive access to the sound card
  224.         'Needed for games
  225.     'DSSCL_NORMAL=cooperates with other apps, shares resources
  226.         'Good for general windows multimedia apps.
  227.     m_ds.SetCooperativeLevel Me.hwnd, DSSCL_PRIORITY
  228. End Sub
  229. Sub LoadWave(i As Integer, sfile As String)
  230.     Dim bufferDesc As DSBUFFERDESC  'a new object that when filled in is passed to the DS object to describe
  231.     'what sort of buffer to create
  232.     Dim waveFormat As WAVEFORMATEX
  233.     'These settings should do for almost any app....
  234.     bufferDesc.lFlags = DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC
  235.     waveFormat.nFormatTag = WAVE_FORMAT_PCM
  236.     waveFormat.nChannels = 2    '2 channels
  237.     waveFormat.lSamplesPerSec = 22050
  238.     waveFormat.nBitsPerSample = 16  '16 bit rather than 8 bit
  239.     waveFormat.nBlockAlign = waveFormat.nBitsPerSample / 8 * waveFormat.nChannels
  240.     waveFormat.lAvgBytesPerSec = waveFormat.lSamplesPerSec * waveFormat.nBlockAlign
  241.     'this next line creates a buffer with the specified file in it, 'BufferDesc' and 'waveformat'
  242.     'describe the properties of the new buffer. they can only be modified when creating a new buffer...
  243.     Set m_dsBuffer = m_ds.CreateSoundBufferFromFile(sfile, bufferDesc, waveFormat)
  244.     'checks for any errors
  245.     If Err.Number <> 0 Then
  246.         MsgBox "unable to find " + sfile
  247.         End
  248.     End If
  249.     'check the panning and volume "properties"
  250.     scrlPan_Change
  251.     scrlVol_Change
  252. End Sub
  253. Private Sub cmdPlay_Click()
  254.     'if there is no sound loaded, load the sound:
  255.     If m_bLoaded = False Then
  256.         m_bLoaded = True
  257.         LoadWave 0, App.Path & "\info.wav"
  258.     End If
  259.             
  260.     Dim flag As Long
  261.     flag = 0
  262.     If chloop.Value <> 0 Then flag = 1  'decide whether or not too loop the sound
  263.     'the play statement has these possibilities
  264.     'dsb_looping=1
  265.     'dsb_default=0
  266.     m_dsBuffer.Play flag
  267. End Sub
  268. Private Sub cmdStop_Click()
  269.     If m_dsBuffer Is Nothing Then Exit Sub 'if the user clicks stop when nothing has been loaded
  270.     'the stop function doesn't 'rewind' the sound back to the beginning
  271.     m_dsBuffer.Stop
  272.     'so we have to tell it to go back to the beginnning
  273.     m_dsBuffer.SetCurrentPosition 0
  274.     'this line ^^ can be used to start a sound 1/2 way through......
  275. End Sub
  276. Private Sub chLoop_Click()
  277.     If chloop.Value = 0 Then
  278.         cmdStop_Click
  279.     End If
  280. End Sub
  281. Private Sub cmdPause_Click()
  282.     If m_dsBuffer Is Nothing Then Exit Sub
  283.     'as stated in the stop section, without the 'setcurrentposition' statement it will not
  284.     'go back to the beggining
  285.     m_dsBuffer.Stop
  286. End Sub
  287. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  288. lblLink1.FontUnderline = False
  289. lbllink2.FontUnderline = False
  290. lbllink3.FontUnderline = False
  291. lblLink1.FontBold = False
  292. lbllink2.FontBold = False
  293. lbllink3.FontBold = False
  294. End Sub
  295. Private Sub lblLink1_Click()
  296. Dim Z As Long
  297. Z = StartDoc("http://www.microsoft.com/directx")
  298. End Sub
  299. Private Sub lblLink1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  300. lblLink1.FontUnderline = True
  301. lbllink2.FontUnderline = False
  302. lbllink3.FontUnderline = False
  303. lblLink1.FontBold = True
  304. lbllink2.FontBold = False
  305. lbllink3.FontBold = False
  306. End Sub
  307. Private Sub lbllink2_Click()
  308. Dim Y As Long
  309. Y = StartDoc("http://www.parkstonemot.freeserve.co.uk/indexfw.htm")
  310. End Sub
  311. Private Sub lbllink2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  312. lblLink1.FontUnderline = False
  313. lbllink2.FontUnderline = True
  314. lbllink3.FontUnderline = False
  315. lblLink1.FontBold = False
  316. lbllink2.FontBold = True
  317. lbllink3.FontBold = False
  318. End Sub
  319. Private Sub lbllink3_Click()
  320. Dim X As Long
  321. X = StartDoc("mailto: jollyjeffers@greenonions.netscapeonline.co.uk")
  322. End Sub
  323. Private Sub lbllink3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  324. lblLink1.FontUnderline = False
  325. lbllink2.FontUnderline = False
  326. lbllink3.FontUnderline = True
  327. lblLink1.FontBold = False
  328. lbllink2.FontBold = False
  329. lbllink3.FontBold = True
  330. End Sub
  331. Private Sub scrlVol_Change()
  332.     If m_dsBuffer Is Nothing Then Exit Sub
  333.     'you can't set the volume value without the buffer being created, so you must handle this
  334.     'otherwise your app will crash.......
  335.     m_dsBuffer.SetVolume scrlVol.Value
  336. End Sub
  337. Private Sub scrlVol_Scroll()
  338.     If m_dsBuffer Is Nothing Then Exit Sub
  339.     m_dsBuffer.SetVolume scrlVol.Value
  340. End Sub
  341. Private Sub scrlPan_Change()
  342.     If m_dsBuffer Is Nothing Then Exit Sub
  343.     'you can't set the panning value without the buffer being created, so you must handle this
  344.     'otherwise your app will crash.......
  345.     m_dsBuffer.SetPan scrlPan.Value
  346. End Sub
  347. Private Sub scrlPan_Scroll()
  348.     If m_dsBuffer Is Nothing Then Exit Sub
  349.     m_dsBuffer.SetPan scrlPan.Value
  350. End Sub
  351.